using UnityEngine;
using System.Collections;

public class AttributesController{

    private bool opened = false; public bool IsOpened(){ return this.opened;}
	
	private static AttributesController instance = null;
	public static AttributesController GetInstance(){
		if (instance == null)
			instance = new AttributesController();
		return instance;
	}
//Constructor	
	private AttributesController(){}
//Miembros	
	private AttributeSet activeAttributeSet;
	private GameObject attributesGUI;
	private GUIPanelWithButtons characterPanel;
	private GUIPanelPerks perksPanel;
	private GUIPanelWithButtons saveResetPanel;
	
//Funciones		
	public void SetCharacterGUI(GameObject go){
		this.attributesGUI = go;
		this.characterPanel = 
			attributesGUI.transform.GetComponentInChildren<GUIPanelWithButtons>();
		this.perksPanel = 
			attributesGUI.transform.GetComponentInChildren<GUIPanelPerks>();
		this.saveResetPanel =
			attributesGUI.transform.FindChild("SavePanel").GetComponent<GUIPanelWithButtons>();
	}
	public void ShowGUI(bool active, bool editMode=false){
		SetActiveCharacter(CharacterDataManager.GetInstance()
		.GetCurrentCharData().attributes);
		Debug.Log(CharacterDataManager.GetInstance().GetCurrentIndex() );
		if (active){
			//ClearSlots();
			this.attributesGUI.SetActive(active);
			//DrawSlots();
		} else{
			this.attributesGUI.SetActive(active);
		}
		UpdateStats();
        characterPanel.EnableEditMode(editMode);
        perksPanel.EnableEditMode(editMode);
		saveResetPanel.gameObject.SetActive(editMode);
	}
    public void ShowStats()
    {
        opened = !opened;
        ShowGUI(opened);
    }
	public void UpdateStats(){
		//Este updatea el index del CharacterData por separado
/*		characterPanel.gameObject.transform.FindChild("CharacterIndex").GetComponent<MyGUIText>().
			SetText(CharacterDataManager.GetInstance().GetCurrentIndex().ToString() );
*/	
		for (int i=0; i<characterPanel.transform.childCount; i++){
			GUIAttribute att = characterPanel.transform.GetChild(i).GetComponent<GUIAttribute>();
			EventAttributeChange ev = characterPanel.transform.GetChild(i).GetComponent<EventAttributeChange>();
			if (att != null){
				if (att.attribute != ATTRIBUTES.NONE){
					att.GUITextValue.SetText("      ");
					att.GUITextValue.SetText(
					activeAttributeSet.GetAttributeTotalValue(att.attribute).ToString() );	
				} else if (att.subAttribute != SUB_ATTRIBUTES.NONE){
					att.GUITextValue.SetText("      ");
					att.GUITextValue.SetText(
					System.Math.Round(activeAttributeSet.GetSubAttributeActualValue(att.subAttribute),2).ToString() );	
				} else if(att.isName){
					//att.GUITextValue.SetText(activeAttributeSet.GetName() );
					att.GUITextValue.SetText("                      ");
					att.GUITextValue.SetText(
					CharacterDataManager.GetInstance().GetCurrentCharData().GetName());
				}
			}else if (ev != null){
				ev.CheckActivation();
			}
		}
		
		characterPanel.transform.FindChild("AvailablePoints").FindChild("Value").
		GetComponent<MyGUIText>().SetText(AttributesController.GetInstance().
		activeAttributeSet.GetAttributePoints().ToString() + "  ");
		//Debug.Log("UNITY DE MIERDA");
	}
	
	public void SetActiveCharacter(AttributeSet att){
		this.activeAttributeSet = att;
		UpdateStats();
	}
	public AttributeSet GetActiveAttributeSet(){return this.activeAttributeSet; }
	public GUIPanelWithButtons GetGUIPanel(){return this.characterPanel;}
	public Camera GetGUICamera(){return this.attributesGUI.GetComponent<Camera>();}
	
}
